package com.android.pagemarket.ui.activity.main;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.android.pagemarket.R;
import com.android.pagemarket.business.logic.InitManager;
import com.android.pagemarket.io.protocol.in.HttpAction.ActionListener;
import com.android.pagemarket.io.protocol.in.LoginAction;
import com.android.pagemarket.io.protocol.out.Company;
import com.android.pagemarket.ui.activity.base.TemplateActivity;
import com.android.pagemarket.ui.widget.CallPopWindow;
import com.android.pagemarket.util.Util;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-10-30
 * @see : 登陆视图 与 已登陆信息视图
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class LoginActivity extends TemplateActivity implements OnClickListener
{

    private EditText com_name;
    private EditText com_num;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(InitManager.getInstance().isLogin() ? R.layout.activity_logininfo : R.layout.activity_login);
        initViews();
    }
        
    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.btn_register:
            {
                RegisterActivity.startActivity(this);
                break;
            }
            case R.id.btn_login:
            {
                doLogin();
                break;
            }            
            case R.id.btn_logout:
            {
                doLogout();
                break;
            }
            case R.id.link_miss_code:
            {
                showMissCodePopWin();
                break;
            }
        }
    }
    
    private void initViews()
    {
        if(InitManager.getInstance().isLogin())
        {
            TextView name = getViewById(R.id.tx_comName);
            TextView num = getViewById(R.id.tx_comNum);
            
            name.setText(InitManager.getInstance().mLoginCompany.companyName);
            num.setText(InitManager.getInstance().mLoginCompany.companyNum);
        }else
        {
            com_name = getViewById(R.id.com_name);
            com_num = getViewById(R.id.com_num);
        }
    }
    
    private void doLogin()
    {
        String name = com_name.getEditableText().toString().trim();
        String num = com_num.getEditableText().toString().trim();
        
        if(name.length() == 0 || !Util.isValidName(name))
        {
            showToast(getString(R.string.tip_name_check));
            return;
        }
        if(num.length() == 0)
        {
            showToast(getString(R.string.tip_num_check));
            return;
        }
        
        LoginAction action = new LoginAction(name, num);
        action.setActionListener(new ActionListener<Company>()
        {
            @Override
            public void onSucceed(Company result)
            {
//                showLoading(false);
                hideLoding();
                if(result.isNoError())
                {
                    InitManager.getInstance().onLogin(result);                    
                    finish();
                }else
                {
                    showToast(R.string.tip_login_fail);
                }
            }
            @Override
            public void onError(int resultCode)
            {
//                showLoading(false);
                hideLoding();
                showToast(LoginAction.getErrorMean(resultCode));
            }
        });
//        showLoading(true);
        showLoding(null, "登录中...");
        action.submit();
    }
    
    @Override
    public void finish()
    {
        Intent data = new Intent();
        data.putExtra("isLogin", InitManager.getInstance().isLogin());
        setResult(RESULT_OK,data );
        super.finish();
    }
    
    private void doLogout()
    {
        InitManager.getInstance().onLogin(null);
        finish();
    }
    
    private void showMissCodePopWin()
    {
        if(getPopupWin() == null)
        {
            CallPopWindow pop = new CallPopWindow(this);
            pop.setTipText(getString(R.string.tip_misscode));
            pop.setPhoneText("");
            pop.setCallPhone(InitManager.getInstance().getServerPhone());
            setPopWin(pop);
        }
        getPopupWin().showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }
    
    public static void startActivity(Context context)
    {
        Intent it = new Intent(context, LoginActivity.class);
        
        context.startActivity(it);
    }
    
    public static void startActivityForResult(Activity context, int requestCode)
    {
        Intent it = new Intent(context, LoginActivity.class);       
        context.startActivityForResult(it, requestCode);
    }
}